﻿@page "/slugify-string"

@rendermode InteractiveAuto
@inject ITranslationService TranslationService
@inject IJSRuntime JSRuntime

<PageTitle>@WebToolInfo.SlugifyTitle - @AppInfo.ToolName</PageTitle>

<PEnqueuedSnackbars @ref="_enqueuedSnackbars"></PEnqueuedSnackbars>

<MRadioGroup @bind-Value="_translationKind"
             Row>
    <MRadio Label="中译英"
            Value="@(TranslationKind.ChineseToEnglish)">
    </MRadio>
    <MRadio Label="英译中"
            Value="@(TranslationKind.EnglishToChinese)">
    </MRadio>
    <MRadio Label="英转别名"
            Value="@(TranslationKind.EnglishToSlug)">
    </MRadio>
    <MRadio Label="中转别名"
            Value="@(TranslationKind.ChineseToSlug)">
    </MRadio>
</MRadioGroup>

<MRow>
    <MButton class="ma-2" OnClick="HandleTranslationAsync">点击转换</MButton>
    <MButton class="ma-2" OnClick="ExecuteCopyAsync">复制</MButton>
</MRow>

<MRow>
    <MCol Sm="10" Md="12">
        <MTextarea Solo class="ma-2" Height="120" BackgroundColor="white" Clearable
                   ClearIcon="mdi-close-circle" Counter="true" AutoGrow @bind-Value="From"/>
    </MCol>
    <MCol Sm="10" Md="12">
        <MTextarea Solo class="ma-2" Height="120" BackgroundColor="grey lighten-2" AutoGrow @bind-Value="_to"/>
    </MCol>
</MRow>

@code {
    TranslationKind _translationKind = TranslationKind.ChineseToSlug;
    private PEnqueuedSnackbars? _enqueuedSnackbars;
    private string? _from;

    private string? From
    {
        get => _from;
        set
        {
            _from = value;
            _ = HandleTranslationAsync();
        }
    }

    private string? _to;

    private void Enqueue(string content, AlertTypes type = AlertTypes.Info)
    {
        _enqueuedSnackbars?.EnqueueSnackbar(new SnackbarOptions
        {
            Content = content,
            Type = type,
            Closeable = true
        });
    }

    public async Task HandleTranslationAsync()
    {
        if (string.IsNullOrWhiteSpace(_from))
        {
            _to = string.Empty;
            return;
        }

        try
        {
            switch (_translationKind)
            {
                case TranslationKind.ChineseToEnglish:
                    _to = await TranslationService!.ChineseToEnglishAsync(_from);
                    break;
                case TranslationKind.EnglishToChinese:
                    _to = await TranslationService!.EnglishToChineseAsync(_from);
                    break;
                case TranslationKind.ChineseToSlug:
                    var english = await TranslationService!.ChineseToEnglishAsync(_from);
                    _to = TranslationService!.EnglishToUrlSlug(english);
                    break;
                default:
                    _to = TranslationService!.EnglishToUrlSlug(_from);
                    break;
            }
        }
        catch (Exception ex)
        {
            _to = ex.Message;
        }
    }

    public async Task ExecuteCopyAsync()
    {
        try
        {
            if (!string.IsNullOrWhiteSpace(_to))
            {
                await JSRuntime.InvokeVoidAsync("clipboardCopy.copyText", _to);
                Enqueue("已复制");
            }
            else
            {
                Enqueue("请先生成后再复制", AlertTypes.Warning);
            }
        }
        catch (Exception ex)
        {
            _to = ex.Message;
        }
    }

}